package controllers

import (
	beego "github.com/beego/beego/v2/server/web"
	"videos/models"
)

type VideoController struct {
	beego.Controller
}

func (c *VideoController) Add() {
	vid := models.Video{
		Title:   c.GetString("title"),
		Des:     c.GetString("des"),
		Image:   c.GetString("image"),
		User:    c.GetString("user"),
		VideoId: 0,
	}
	err := models.DB.Create(&vid).Error
	if err != nil {
		c.Data["json"] = models.Msg{
			Code: 0,
			Mas:  "服务器繁忙",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.Msg{
		Code: 200,
		Mas:  "添加成功",
		Data: nil,
	}
	c.ServeJSON()
}
func (c *VideoController) List() {
	c.Ctx.Output.Header("Access-Control-Allow-Origin", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Methods", "*")
	c.Ctx.Output.Header("Access-Control-Allow-Headers", "*")
	if c.Ctx.Input.Method() == "OPTIONS" {
		c.Ctx.Output.SetStatus(200)
		c.StopRun()
	}
	name := c.GetString("name")
	vs := []models.Video{}
	sql := models.DB
	if name != "" {
		sql = sql.Where("title like ?", "%"+name+"%")
	}
	err := sql.Find(&vs).Error
	if err != nil {
		c.Data["json"] = models.Msg{
			Code: 0,
			Mas:  "服务器繁忙",
			Data: nil,
		}
		c.ServeJSON()
		return
	}
	c.Data["json"] = models.Msg{
		Code: 200,
		Mas:  "查询成功",
		Data: vs,
	}
	c.ServeJSON()
}
